// pages/famousMaster/famousMaster.js
var app=getApp();
var util=require("../../utils/util");
var config=require("../../api/config");
var home=require("../../api/home");


Page({
  /*页面的初始数据*/
  data: {
    width: 0,
    navColor: '#fff',
    title: '名师专栏',
    top_height: 0,
    capsuleHeight: 44,
    sub_left_list: [],
    sub_right_list: [],
    cur_left_sub: 0,
    cur_right_sub: 10000,
    sub_name: '',
    sub_id: '',
    class_list:[],
    class_sub_list:[],
    cur_nav: -1,
    class_id: '',
    grade_list: [],
    grade_name: '',
    grade: 0,
    grade_item: '',
    title_index: null,
    item_index: null,
    tec_list: [],
    inst_show: true,
    sub_show: true,
    city: '',
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    show_nav: false,
    educations:['小学','初中','高中','专科','本科','硕士','博士']
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var sysInfo = wx.getSystemInfoSync(),pixelRate = sysInfo.windowWidth / 750;
    var city=wx.getStorageSync('city');
    that.setData({top_height: (sysInfo.statusBarHeight+44)/pixelRate,width:sysInfo.windowWidth,city:city,pixelRate:pixelRate, statusBarHeight:sysInfo.statusBarHeight})
    //
    that.get_all_data()
  },
  nav_home:function(){
    wx.navigateBack({
      delta: 1
    });
  },
  // 年级选择
  open_inst:function(){
    this.setData({inst_show:!this.data.inst_show})
  },
  close_inst:function(){
    this.setData({inst_show:!this.data.inst_show})
  },
  check_inst:function(e){
    var that=this,title_index=that.data.title_index,item_index=that.data.item_index;
    var index=e.currentTarget.dataset.index,idx=e.currentTarget.dataset.idx,item=e.currentTarget.dataset.item;
    if(title_index==index&&item_index==idx){
      that.setData({title_index:null,item_index:null,grade_item:''})
    }else{
      that.setData({title_index:index,item_index:idx,grade_item:item})
    }
  },
  confirm_inst:function(){
    var that=this,grade=that.data.grade,grade_item=that.data.grade_item,sub_id=that.data.sub_id,class_id=that.data.class_id;
    if(grade_item){
      if(grade!=grade_item.value){
        that.setData({grade:grade_item.value,grade_name:grade_item.text,tec_list:[],currentPage:1,show_nav:false})
      }
    }else{
      that.setData({grade:0,grade_name:'全部年级',tec_list:[],currentPage:1,show_nav:false})
    }
    
    that.close_inst()
    that.get_teach_list(sub_id,class_id)
  },
  // 分类弹窗
  open_sub:function(){
    this.setData({sub_show:!this.data.sub_show})
  },
  close_sub:function(){
    this.setData({sub_show:!this.data.sub_show})
  },
  // 兴趣教育/学科辅导/全部
  check_left_sub:function(e){
    var that=this,sub_left_list=that.data.sub_left_list;
    var index=e.currentTarget.dataset.index;
    if(index==1){
      var id=sub_left_list[1].id;
      that.setData({sub_id:2,cur_right_sub:sub_left_list[1].cur_right_sub,cur_left_sub:index})
      if(sub_left_list[1].list){
        that.setData({sub_right_list:sub_left_list[1].list})
      }else{
        that.get_cate_child_list(id,2)
      }
    }else if(index==0){
      that.setData({sub_id:1,sub_right_list:sub_left_list[0].list,cur_right_sub:sub_left_list[0].cur_right_sub,cur_left_sub:index})
    }else{
      sub_left_list[0].cur_right_sub=10000
      sub_left_list[1].cur_right_sub=10000
      that.setData({sub_id:-1,class_id:-1,cur_right_sub:10000,cur_left_sub:0,sub_name:'全部',tec_list:[],currentPage:1,show_nav:false,sub_left_list:sub_left_list,sub_right_list:sub_left_list[0].list})
      that.get_teach_list(-1,-1)
      that.close_sub();
    }
  },
  // 分类选择
  check_right_sub:function(e){
    var that=this,sub_right_list=that.data.sub_right_list,cur_left_sub=that.data.cur_left_sub,sub_left_list=that.data.sub_left_list,class_sub_list=that.data.class_sub_list;
    var index=e.currentTarget.dataset.index,id=e.currentTarget.dataset.id,arr=[],cur_index=0;
    that.close_sub()
    if(cur_left_sub==1){
      sub_left_list[0].cur_right_sub=10000;
      if(sub_left_list[1].cur_right_sub==index){
        return false;
      }else{
        sub_left_list[1].cur_right_sub=index
      }
      if(sub_right_list[index].name=="全学科"){
        arr=class_sub_list;
        id=-1;
        for(var i=0;i<class_sub_list.length;i++){
          if(class_sub_list[i].name=="全学科"){cur_index=i}
        }
        that.setData({cur_nav:cur_index})
        // setTimeout(()=>{that.scroll_left(class_sub_list.length,cur_index)},200)
      }else{
        arr.push(sub_right_list[index])
        that.setData({cur_nav:0})
        // setTimeout(()=>{that.setData({scrollLeft:0})},200)
      }
      that.setData({sub_left_list:sub_left_list,cur_right_sub:index,sub_id:2,sub_name:sub_right_list[index].name,tec_list:[],currentPage:1,show_nav:false,class_list:arr,class_id:id})
      that.get_teach_list(2,id)
    }else{
      sub_left_list[1].cur_right_sub=10000;
      if(sub_left_list[0].cur_right_sub==index){
        return false;
      }else{
        sub_left_list[0].cur_right_sub=index
      }
      that.setData({sub_left_list:sub_left_list,cur_right_sub:index,sub_name:sub_right_list[index].name,sub_id:1,cur_nav:-1,scrollLeft:0,tec_list:[],currentPage:1,show_nav:false})
      //
      // that.get_cate_child_list(id,1)
      that.get_teach_list(1,id)
    }
  },
  // 小分类
  // choose_nav: function (e) {
  //   var that=this,cur_nav=that.data.cur_nav,len=that.data.class_list.length,sub_id=that.data.sub_id;
  //   var index=e.currentTarget.dataset.index,id=e.currentTarget.dataset.id;
  //   if(cur_nav!=index){
  //     that.setData({cur_nav:index,class_id:id,tec_list:[],currentPage:1,show_nav:false})
  //     // 数据
  //     that.get_teach_list(sub_id,id)
  //   }
  //   //滑动
  //   that.scroll_left(len,index)
  // },
  // scroll_left:function(len,index){
  //   var that=this;
  //   if(len>=3){
  //     var curWidth=(that.data.width-30)/5;
  //     var moveWidth=(index-1)*curWidth
  //     if(moveWidth>0){
  //       that.setData({scrollLeft:moveWidth})
  //     }else{
  //       that.setData({scrollLeft:0})
  //     }
  //   }
  // },
  return:function(){},
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,sub_id=that.data.sub_id,class_id=that.data.class_id;
    if(hasMoreData){
      that.get_teach_list(sub_id,class_id)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 获取数据
  get_all_data:function(){
    var that=this;
    //
    that.get_grade_list();
    that.get_cate_list();
  },
  // 在读年级列表
  get_grade_list:function(){
    var that=this;
    config.get_grade_list().then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        that.setData({grade_list: list,grade_name:'全部年级',grade:0})
      }
    })
  },
  // 获取分类
  get_cate_list:function(){
    var that=this;
    home.get_home_classify_list().then(res=>{
      if(res.data.code==200){
        var list=res.data.data,sub_left_list=[{name:'兴趣教育',list:[],cur_right_sub:10000}];
        for(var i=0;i<list.length;i++){
          if(list[i].name=="附近机构"){
            list.splice(i,1)
          }
          if(list[i].name=="学科辅导"){
            sub_left_list.push(list.splice(i,1)[0])
          }
        }
        sub_left_list[0].list=list;
        sub_left_list[1].cur_right_sub=10000;
        that.setData({sub_left_list:sub_left_list,sub_right_list:list,sub_name:"全部",sub_id:-1})
        // that.get_cate_child_list(list[0].id,1)
        that.get_teach_list(-1,-1)
      }
    })
  },
  // 获取子分类
  get_cate_child_list:function(id,type){
    wx.showLoading({title: '加载中'})
    var that=this,sub_left_list=that.data.sub_left_list;
    var params={
      "parentId": id,
      "flagEnable": 1
    }
    home.get_cate_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data,arr=[];
        that.setData({class_list: list,class_id:-1,tec_list:[],currentPage:1,show_nav:false})
        if(type==2){
          for(var i=0;i<list.length;i++){
            if(list[i].name=="语文"||list[i].name=="数学"||list[i].name=="英语"||list[i].name=="全学科"){
              arr.push(list[i])
            }
          }
          sub_left_list[1].list=arr;
          that.setData({sub_right_list:arr,sub_left_list:sub_left_list,class_sub_list:list})
        }else{
          //
          that.get_teach_list(id,-1)
        }
      }
    })
  },
  // 获取名师列表
  get_teach_list:function(tid,sid){
    wx.showLoading({title: '加载中'})
    var that=this,grade=that.data.grade,currentPage=that.data.currentPage,pageSize=that.data.pageSize,city=that.data.city,cur_left_sub=that.data.cur_left_sub,educations=that.data.educations;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "teachingType": tid<0?'':tid,
      "subjectType": sid<0?'':sid,
      "grade": grade>0?grade:'',
      "lat": city.lat,
      "lng": city.lng
    }
    // if(sid<0){delete params.subjectType}
    if(cur_left_sub!=1){delete params.grade}
    home.get_teach_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.data;
        for(var i=0;i<list.length;i++){
          list[i].realName=list[i].realName?(list[i].realName.indexOf("老师")>-1?list[i].realName:list[i].realName+"老师"):'';
          list[i].yearCount=list[i].teachingTime?new Date().getFullYear()-new Date(list[i].teachingTime).getFullYear()+1:'';
          list[i].count=list[i].studentCount>10000?Math.ceil(list[i].studentCount/10000)+'万':list[i].studentCount;
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          list[i].educaName=list[i].education?educations[Number(list[i].education)-1]:'';
          list[i].positiveRate=list[i].positiveRate?list[i].positiveRate*100+'%':'';
        }
        var tec_list = that.data.tec_list;
        tec_list = util.extend(tec_list, list)
        if (list.length < pageSize) {
          that.setData({
            tec_list: tec_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            tec_list: tec_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&tec_list.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
})